var twitpeek = 
{
  twitter_search: "http://search.twitter.com/search.json",
  twitter_query: "?q=twitpic.com%20OR%20yfrog.com%20OR%20twitgoo.com%20OR%20tweetphoto.com",
  delay_by: 2.5,
  get_amount: 30,
  twits_array: [],
  
  init: function()
  {
    twitpeek.poll_twitter();
    twitpeek.thumb_writer_interval();
  },
  
  poll_twitter: function()
  {
    console.log("Poll");
    var def = dojo.io.script.get({
        url: twitpeek.twitter_search + twitpeek.twitter_query + "&rpp=" + twitpeek.get_amount,
        callbackParamName: 'callback',
        timeout: 3000,
      });
    def.addCallback(twitpeek.twitter_handler);
    def.addErrback(twitpeek.twitter_failwhale);
  },
  twitter_failwhale: function(fail)
  {
    console.log("Twitter just failed: " + fail);
    setTimeout(twitpeek.poll_twitter, 3000);    
  },
  twitter_handler: function(data)
  {
    try
    {
      console.log("Handler" + data.results.length);      
      twitpeek.twitter_query = data.refresh_url;
      twitpeek.update_twits_array(data.results);
    } catch (e) {console.log("Err: " + e);}
  },
  
  update_twits_array: function(twits)
  {
    try
    {
      var temp = twitpeek.twits_array.length;
      for (var i = 0; i < twits.length; i++) 
      {
        twitpeek.twits_array.push(twits[i]);
      }
    } catch (e) {console.log("Err: " + e);}
  },
  
  make_thumb_html: function(thumb)
  {
    var thumb_array = twitpeek.make_thumb_array(thumb.text);
    var html = "";
    if (thumb_array)
    {
      switch (thumb_array[1])
      {
        case "twitpic":
          thumbUrl = "http://twitpic.com/show/mini/" + thumb_array[2];
          break;
        case "/yfrog":
          thumbUrl = "http://yfrog.com/" + thumb_array[2] + ".th.jpg";
          break;
        case "twitgoo":
          thumbUrl = "http://twitgoo.com/show/thumb/" + thumb_array[2];
          break;
        case "tweetphoto":
          thumbUrl = "http://www.tweetphoto.com/show/mini/" + thumb_array[2];
          break;
      }
      html = "<a href=\"http://" + thumb_array[0] + "\" class=\"thumb\" id=\"" + thumb_array[2] + "\" target=\"_blank\">";
      html += "<img src=\"" + thumbUrl + "\" alt=\"" + thumb_array[1] + "\">";
      html += "</a>";
    }
    return html;
  },
  
  make_thumb_array: function(text)
  {
    // you know you want this
    var re = new RegExp("(mileycyrus|miley cyrus|miley|mileyc)","i");
    var crap = (re.exec(text));
    if (crap)
      return null;

    re = new RegExp("(twitpic|\/yfrog|twitgoo|tweetphoto).com/([A-Za-z0-9_]*)");
    var results = (re.exec(text));
    return results;
  },
  
  thumb_writer_interval: function()
  {
      setTimeout(twitpeek.thumb_writer, (twitpeek.delay_by * 1000));
  },
  
  thumb_writer: function()
  {
    var thumb = twitpeek.twits_array.shift();
    if (thumb) 
    {
      var html = twitpeek.make_thumb_html(thumb);
      if (html) 
      {
        try
        {
          dojo.place(html, dojo.byId("thumbs"), "first");
          while (dojo.byId("thumbs").childNodes.length > 57) 
            dojo.destroy(dojo.byId("thumbs").lastChild);
        } catch (e) {console.log("Err: " + e);}
      }
    }
    //if buffer is at 15 left, start poll_twitter
    if (twitpeek.twits_array.length == 15) 
      setTimeout(twitpeek.poll_twitter, 1000);
    twitpeek.thumb_writer_interval();
  }
};